from cn.dbswitcher.tools.IdGenerator import gen
from mongoengine import *

from cn.dbswitcher.formatter.ContentTypeFormatter import *


class Content(DynamicDocument):
    cid = StringField(max_length=200, primary_key=True)
    _class = StringField(max_length=200, required=True)
    title = StringField(max_length=200, required=True)
    content = StringField(required=True)
    createTime = DateTimeField(required=True)
    createUser = StringField(max_length=200, required=True)
    intro = StringField(max_length=300, required=True)
    hitCount = IntField(required=True)
    pic = StringField(max_length=400, required=True)
    typeId = StringField(max_length=200, required=True)
    typeName = StringField(max_length=200, required=True)
    isDeleted = BooleanField(required=True)
    weight = IntField(required=True)
    oldNo = StringField(max_length=50, required=True)
    extendPropAndVal = DictField()
    meta = {'collection': 'content', 'db_alias': 'mongo222'}

    # 怀孕知识库
    def knhySql(self):
        return "SELECT kn.id, CASE WHEN item.Week IS NULL THEN kn.YunWeek ELSE item.Week END AS week, CASE WHEN item. DAY IS NULL THEN 0 ELSE item. DAY % 7 END AS DAY, kn.Title, CASE WHEN item.Poster IS NULL THEN '' ELSE item.Poster END AS poster, CASE WHEN item.Weight IS NULL THEN CASE WHEN kn.SortNum IS NULL THEN 0 ELSE kn.SortNum END ELSE item.Weight END AS weight, CASE WHEN item.typeId IS NULL THEN 'knowledge_huaiyun' ELSE item.TypeId END TypeId, CASE WHEN item.typeId IS NULL THEN '孕周知识' ELSE httpMethod.Name END AS typename, kn.content AS content, CASE WHEN item.Summary IS NULL THEN '无简介' ELSE item.Summary END AS intro, kn.AddTime, kn.HitNum, kn.createUser FROM RecommendType AS httpMethod LEFT JOIN RecommendItem item ON item.TypeId = httpMethod.id LEFT JOIN Knowledges AS kn ON item.ObjectId = kn.id WHERE httpMethod.Type = 2"

    def knhyContent(self, ret):
        return Content(cid=gen("C"), oldNo=ret[0], title=ret[3], pic=ret[4], weight=ret[5],
                       typeId=knHYFormatter(ret[6]),
                       typeName=ret[7],
                       content=ret[8], intro=ret[9][0:300], createTime=ret[10], hitCount=ret[11], createUser=ret[12],
                       isDeleted=False, extendPropAndVal={"时间单位": "周", "时间数": ret[1], "孕天": ret[2]},
                       _class='cn.ibabygroup.cms.domainmodels.Content')

    # 能不能吃
    def noteatSql(self):
        return "SELECT ne.title, ne.pic, ne.SortNum, ne.TypeId, httpMethod.CategoryName, ne.Intro, ne.HitNum, ne.AddTime, CASE WHEN ne.FoodInfo IS NULL THEN '' ELSE ne.FoodInfo END AS FoodInfo, ne.YunSupport, ne.YunSupportInfo, ne.ConfinementSupport, ne.ConfinementSupportInfo, ne.NursingSupport, ne.NursingSupportInfo, ne.BabySupoport, ne.BabySupoportInfo,ne.id FROM KnowledgesNoEat ne JOIN KnowledgeType httpMethod ON TypeId = httpMethod.id"

    def notEatContent(self, ret):
        return Content(oldNo=ret[17], cid=gen("C"), title=ret[0], pic=ret[1], weight=ret[2],
                       typeId=notEetFormatter(ret[3]),
                       typeName=ret[4],
                       content='', intro=ret[5], hitCount=ret[6], createTime=ret[7], createUser='admin',
                       isDeleted=False, extendPropAndVal={"食谱": ret[8], "孕妇是否能吃": notEatPro(ret[9]), "孕妇说明": ret[10],
                                                          "月子是否能吃": notEatPro(ret[11]), "月子说明": ret[12],
                                                          "哺乳是否能吃": notEatPro(ret[13]), "哺乳说明": ret[14],
                                                          "婴儿是否能吃": notEatPro(ret[15]), "婴儿说明": ret[16]},
                       _class='cn.ibabygroup.cms.domainmodels.Content')

    # 每日推荐
    def indexrecommend(self):
        return "SELECT * FROM ( SELECT rt.Type, ri.Week, ri. DAY % 7 AS DAY, ri.Title, CASE WHEN ri.Poster IS NULL THEN '' ELSE ri.Poster END AS pic, CASE WHEN ri.Weight IS NULL THEN 0 ELSE ri.Weight END AS weight, rt.Id TypeId, rt.Name, '' AS content, ri.Summary, ri.Created, 0 AS hitCount, 'admin' AS createUser FROM RecommendType AS rt INNER JOIN RecommendItem AS ri ON rt.Id = ri.TypeId ) t WHERE Type = 1"

    def indexContent(self, ret):
        return Content(cid=gen("C"), title=ret[3], pic=ret[4], weight=ret[5], typeId=knHYFormatter(ret[6]),
                       typeName=ret[7],
                       content=ret[8], intro=ret[9][0:300], createTime=ret[10], hitCount=ret[11], createUser=ret[12],
                       isDeleted=False, extendPropAndVal={"时间单位": "周", "时间数": ret[1], "孕天": ret[2]},
                       _class='cn.ibabygroup.cms.domainmodels.Content')

        # 普通知识库

        # 查询所有非孕妈知识库绑定的知识库文章 "SELECT kn.id, kn.YunWeek, '0' AS DAY, kn.Title, CASE WHEN kn.Poster IS NULL THEN '' ELSE kn.Poster END AS pic, CASE WHEN kn.SortNum IS NULL THEN 0 ELSE kn.SortNum END AS weight, 'knowledge_huaiyun_remenzhishi' AS typeid, '热门知识' AS typename, kn.content, kn.title AS intro, kn.AddTime, kn.HitNum, kn.CreateUser, CASE WHEN kt.CategoryName IS NULL THEN '' ELSE kt.CategoryName END AS subtype FROM Knowledges kn LEFT JOIN KnowledgeType kt ON kn.CateId = kt.Id WHERE kn.id NOT IN ( SELECT kn.id FROM RecommendType AS httpMethod LEFT JOIN RecommendItem item ON item.TypeId = httpMethod.id LEFT JOIN Knowledges AS kn ON item.ObjectId = kn.id WHERE httpMethod.Type = 2 )"
        # 按子类型分级查询未绑定的知识库文章 "SELECT * FROM ( SELECT kn.id, kn.YunWeek, '0' AS DAY, kn.Title, CASE WHEN kn.Poster IS NULL THEN '' ELSE kn.Poster END AS pic, CASE WHEN kn.SortNum IS NULL THEN 0 ELSE kn.SortNum END AS weight, CASE WHEN kn.CateId = '0' THEN 'knowledge_huaiyun_remenzhishi' ELSE kn.CateId END AS typeId, CASE WHEN kt.CategoryName IS NULL THEN '热门知识' ELSE kt.CategoryName END AS typename, kn.content, kn.title AS intro, kn.AddTime, kn.HitNum, kn.CreateUser, CASE WHEN kt.type IS NULL THEN 2 ELSE kt.type END type, kt.fid FROM Knowledges kn LEFT JOIN KnowledgeType kt ON kn.CateId = kt.Id WHERE kn.id NOT IN ( SELECT kn.id FROM RecommendType AS httpMethod LEFT JOIN RecommendItem item ON item.TypeId = httpMethod.id LEFT JOIN Knowledges AS kn ON item.ObjectId = kn.id WHERE httpMethod.Type = 2 )) a WHERE a.type <> 3"

    def normalKn(self):
        return "SELECT kn.id, kn.YunWeek, '0' AS DAY, kn.Title, CASE WHEN kn.Poster IS NULL THEN '' ELSE kn.Poster END AS pic, CASE WHEN kn.SortNum IS NULL THEN 0 ELSE kn.SortNum END AS weight, 'knowledge_huaiyun_remenzhishi' AS typeid, '热门知识' AS typename, kn.content, kn.title AS intro, kn.AddTime, kn.HitNum, kn.CreateUser, CASE WHEN kt.CategoryName IS NULL THEN '' ELSE kt.CategoryName END AS subtype FROM Knowledges kn LEFT JOIN KnowledgeType kt ON kn.CateId = kt.Id WHERE kn.id NOT IN ( SELECT kn.id FROM RecommendType AS httpMethod LEFT JOIN RecommendItem item ON item.TypeId = httpMethod.id LEFT JOIN Knowledges AS kn ON item.ObjectId = kn.id WHERE httpMethod.Type = 2 )"

    def normalContent(self, ret):
        return Content(oldNo=ret[0], cid=gen("C"), title=ret[3], pic=ret[4], weight=ret[5],
                       typeId=ret[6],
                       typeName=ret[7],
                       content=ret[8], intro=ret[9], createTime=ret[10], hitCount=ret[11], createUser=ret[12],
                       isDeleted=False, extendPropAndVal={"时间单位": "周", "时间数": ret[1], "孕天": ret[2]},
                       _class='cn.ibabygroup.cms.domainmodels.Content')

    def tempBeiyunSql(self):
        return "SELECT kn.id, kn.title, kn.SortNum,kn.content, kn.AddTime, kn.HitNum, kn.CreateUser FROM Knowledges kn  JOIN KnowledgeType ty ON kn.CateId = ty.Id WHERE kn.title LIKE '%备孕%'"

    def tempBeiyunContent(self, ret, database):
        content = Content(oldNo=ret[0], cid=gen("C"), title=ret[1], pic="", weight=ret[2],
                          typeId='knowledge_beiyun',
                          typeName='备孕',
                          content=ret[3], intro=ret[1], createTime=ret[4], hitCount=ret[5], createUser=ret[6],
                          isDeleted=False, extendPropAndVal={},
                          _class='cn.ibabygroup.cms.domainmodels.Content')
        content.switch_db(database)
        return content
